﻿package DevilCat{
	import flash.display.MovieClip ;
	import flash.events.Event;  
	import flash.display.Loader;
	import flash.net.URLRequest; 
	import flash.text.TextField ;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent; 
	import DevilCat.PokemonObject;
	import DevilCat.SQLData;
	public class NPCMessenger extends MovieClip  {
		public var tweenList:Array = new Array();
		public var pid:Number;
		public var eventLocation:Number=0;
		public var eventNumber:Number=0;
		public var eventChange:Boolean=true;
		public var wasActive:Boolean=false;
		public var actiBox:Boolean=false;
		public function NPCMessenger(XMLData:XMLList){
			NPCMessBox.Content.text=String(XMLData.mes);
			//autoSize
			NPCMessBox.NPCMessBoxBG.height=20*Number(NPCMessBox.Content.numLines);
			NPCMessBox.y= -44 -Number(NPCMessBox.Content.numLines - 1)*20;
			NPCMessBox.visible=false;
			if(String(XMLData.char)!="")
				gotoAndStop(String(XMLData.char));
			if(String(XMLData.evc)!="")
				eventChange=false;
			setEvent(Number(XMLData.evl),Number(XMLData.evn));
		}
//		private function loadPokemon(pid:Number) {
//			var imageLoader:Loader = new Loader();
//			var imageLink:URLRequest = new URLRequest("http://dex.poke-mega.org/image/pokemon/"+pid+".png");
//			imageLoader.load(imageLink);
//			imageLoader.contentLoaderInfo.addEventListener(Event.INIT,imageLoaded);
//			function imageLoaded(e:Event):void {
//				addChild(imageLoader);
//			}
//		}
		public function setEvent(eventLocationB:Number,eventNumberB:Number){			
			if(eventLocationB>0){
				eventLocation=eventLocationB;
				eventNumber=eventNumberB;			
			}
		}
		public function addMe(){
			tweenList.unshift(new Tween (this,"alpha",Strong.easeOut,0,1,2,true));
			tweenList.unshift(new Tween (this,"y",Strong.easeOut,this.y-50,this.y,2,true));
		}
		public function hitMe(){
			NPCMessBox.visible=true;
			if(!wasActive && eventLocation>0 && eventChange){//Kiểm tra xem đã nhận Event chưa, nếu chưa mới set Event
				SQLData.changeUserEvent(eventLocation,eventNumber)
			}
			wasActive=true;
//			actiBox=true;
//			var NPCMessBoxX:int=69;
//			tweenList.unshift(new Tween (NPCMessBox,"x",Strong.easeOut,NPCMessBoxX+20,NPCMessBoxX,2,true));
//			tweenList.unshift(new Tween (NPCMessBox,"y",Strong.easeOut,NPCMessBox.y,NPCMessBox.y,5,true));
		}
		public function unHitMe(){			
			NPCMessBox.visible=false;
//			if(NPCMessBox.visible){
//				tweenList.unshift(new Tween (NPCMessBox,"alpha",Strong.easeOut,1,0,2,true));
//				tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,removeBox);
//			}
		}
		public function removeBox(e:Event){
			NPCMessBox.visible=false;
			actiBox=false;
		}
		public function removeMe(e:Event){
			if(this.parent != null){
				this.parent.removeChild(this);
				//trace ("Removed");
			}
		}
	}
}
	